package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.entity.Suppliers;
import com.dingreading.cloud.admin.service.GoodsSuppliersService;
import com.dingreading.cloud.admin.service.SuppliersService;
import com.dingreading.cloud.admin.service.WhPurchaseService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.google.common.collect.Lists;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/system/suppliers")
@Api(tags = "总部-商品供应商管理")
public class SuppliersController {

    @Resource
    protected SuppliersService suppliersService;
    @Resource
    protected GoodsSuppliersService goodsSuppliersService;
    @Resource
    protected WhPurchaseService whPurchaseService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "供应商名") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "是否禁用 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        Page<Suppliers> page = this.suppliersService.pageList(pageUtil, name, enabled);

        PageBeanDto data = new PageBeanDto();
        data.setTotal(page.getTotalRow());
        data.setList(page.getRecords());
        return R.ok(data);
    }


    @ApiOperation(value = "添加/编辑商品供应商")
    @PostMapping(value = "/editBean", produces = "application/json; charset=UTF-8")
    public R<Object> editBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "商品供应商名") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "状态 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        if (id == null || id <= 0) {
            // 新增
            Suppliers data = newSuppliers(new Suppliers(), name, enabled);
            suppliersService.saveOrUpdate(data);
        } else {
            // 修改
            Suppliers data = suppliersService.getById(id);
            if (data == null) return R.fail("获取失败");

            newSuppliers(data, name, enabled);
            suppliersService.saveOrUpdate(data);
        }
        return R.ok();
    }

    /**
     * 新建供应商
     */
    public static Suppliers newSuppliers(Suppliers data, String name, Integer enabled) {
        if (data.getId() != null && data.getId() > 0) {

        } else {
            data.setEnabled(1);
        }

        if (name != null)
            data.setName(name);
        if (enabled != null)
            data.setEnabled(enabled);
        return data;
    }


    @ApiOperation(value = "获取商品供应商集合")
    @PostMapping(value = "/getList", produces = "application/json; charset=UTF-8")
    public R<Object> getList(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "商品供应商名") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "状态 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        List<Suppliers> list = this.suppliersService.getByEnabled(name, enabled);
        return R.ok(list);
    }


    @ApiOperation(value = "修改启用状态")
    @PostMapping(value = "/updateEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateEnabled(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id,
            @ApiParam(value = "启用状态 0.禁用 1.启用") @RequestParam(value = "enabled") Integer enabled
    ) {
        boolean l = this.suppliersService.updateEnabled(Lists.newArrayList(id), enabled);
        if (!l) return R.fail("修改失败");
        goodsSuppliersService.updateEnabledBySupplierId(id, enabled);
        return R.ok();
    }


    @ApiOperation(value = "获取商品供应商详情")
    @PostMapping(value = "/getBean", produces = "application/json; charset=UTF-8")
    public R<Object> getBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        Suppliers data = suppliersService.getById(id);
        if (data == null) return R.fail("获取失败");

        return R.ok(data);
    }


    @ApiOperation(value = "删除商品供应商")
    @PostMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        Suppliers data = suppliersService.getById(id);
        if (data == null) return R.fail("获取失败");

        long supplierCnt = whPurchaseService.getSupplierCnt(id);
        if (supplierCnt > 0) return R.fail("已有该供应商关联的采购信息，不能删除");

        long l = suppliersService.dropData(id);
        if (l <= 0) return R.fail("删除失败");
        goodsSuppliersService.dropDataBySupplierId(id);
        return R.ok();
    }

}